﻿using Bilgesoft.Framework.Server.DatabaseLayer;
using Bilgesoft.Framework.Server.DomainObjects;
using Bilgesoft.Framework.Shared.DataObjects;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bilgesoft.Framework.Server.Security.Authentication
{
    public class AuthenticationManager
    {
        private static AuthenticationManager _manager;
        public static AuthenticationManager Current
        {
            get
            {
                if (_manager == null)
                    _manager = new AuthenticationManager();
                return _manager;
            }
        }


        private AuthenticationManager()
        {

        }

        private const string CONNECTION_STRING = "Framework";
        public UserDTO validate(string username, string password)
        {
            using (DatabaseContext db = DatabaseContext.CreateContext(CONNECTION_STRING))
            {
                DbSet<User> users = db.Set<User>();
                return (from x in users
                                where x.UserName == username && x.Password == password
                                select new UserDTO
                                {
                                    Email = x.Email,
                                    Password = x.Password,
                                    UserName = x.UserName
                                }).FirstOrDefault();
            }
        }
    }
}
